package sg.edu.nus.iss.cats.controller;

import java.io.*;
import javax.servlet.http.*;

import sg.edu.nus.iss.cats.model.*;

public class HTMLUtil{

   //Print page header    
   public static void displayPageHeader(HttpServletRequest request,
           HttpServletResponse response) throws IOException{
        
        PrintWriter out = response.getWriter();
        
        out.println("<html>\n");
        
        out.println("<head>" + 
                    "<link rel=\"STYLESHEET\" type=\"text/css\" href=\"" + request.getContextPath() + "/css/style.css\">" + 
                    "</head>\n"); 
        
        out.println("<body>\n");
            
        out.println("<table class=\"framed\" align=\"center\" width=\"100%\" " + 
                    "cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n" + 
                    "<tr>\n" + 
                    "<td><img src=\"" + request.getContextPath() +"/img/logoSmall_2.jpg\" width=\"155\" height=\"66\" border=\"0\"></td>\n" +"<td align=\"center\"><h1>Course Application Tracking System</h1></td>\n" +
                    "<td>\n" +
                    "</td>\n" + 
                    "</tr></table>\n" +
                    "<h2>Welcome to CATS</h2>\n");
    }
    
    //Print page header    
    public static void displayPageFooter(HttpServletRequest request,
           HttpServletResponse response) throws IOException{

            PrintWriter out = response.getWriter();            
            out.println("</body></html>\n");


    }
    
   public static void displayLoginForm(HttpServletRequest request,
          HttpServletResponse response) throws IOException{
                      
       PrintWriter out = response.getWriter();
       
       
       out.println("<form name=\"logonForm\" method=\"post\" action=\"" + request.getContextPath() + "/login\" focus=\"username\">\n" + 
                    "<table class=\"framed\">\n" + 
                    "<tr>\n" + 
                        "<td>Userid</td>\n" +
                        "<td colspan=\"3\"><input type=\"text\" name=\"username\" size=\"40\" maxlength=\"40\"/></td>\n" + 
                    "</tr>\n" + 
                    "<tr>\n" + 
                        "<td>Password</td>\n" +                         "<td colspan=\"3\"><input type=\"password\" name=\"password\" size=\"40\" maxlength=\"40\"/></td>\n" + 
                    "</tr>\n" +
                    "<tr><td colspan=\"4\">&nbsp;</td></tr>\n" + 
                    "<tr>\n" +
                    "<td>&nbsp;</td>\n" + 
                    "<td>\n" + 
                    "<button name=\"submit\" type=\"submit\" value=\"s\">\n" + 
                    "<img src=\"" + request.getContextPath() + "/img/button_login.gif\" alt=\"Login\" align=\"middle\">\n" + 
                    "</button>\n" + 
                    "</td>\n" + 
                    "<td>\n" + 
                    "<button name=\"clear\" type=\"reset\" value=\"r\">\n" + 
                    "<img src=\"" + request.getContextPath() +"/img/button_clear.gif\" alt=\"Reset\" align=\"middle\">\n" +
                    "</button>\n" + 
                    "</td>\n" + 
                    "</tr>\n" + 
                    "</table>\n" + 
                    "</form>\n");  
        out.println("<script type=\"text/javascript\" language=\"JavaScript\">\n" +
                      "<!--\n" +
                      "var focusControl =\n" +
                      "document.forms[\"logonForm\"].elements[\"username\"];\n" +

                      "if (focusControl.type != \"hidden\") {\n" +
                         "focusControl.focus();\n" +
                      "}\n" +
                      "// -->\n" +
                    "</script>\n");
                    
                    
   }

    //Get userId from the servlet pathInfo    
    public static String stripUserId(String pathInfo){

        int pos = pathInfo.indexOf("/");
        int len = pathInfo.length();
        String userId = pathInfo.substring(pos+1,len);

        System.out.println("UserId: " + userId);

        return userId;



    }
    
  
    
   public static void displayUsers(HttpServletRequest request,
           HttpServletResponse response, User[] users) throws IOException{
           
            PrintWriter out = response.getWriter();
            displayPageHeader(request,response);
        
            out.println("<h2>Current User List</h2>\n");
            out.println("<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n" +                                 
                        "<tr><th>User Id</th><th>Name</th><th>Employee Id</th><th>Roles</th></tr>\n");
            
            System.out.println("Printing details of : " + users.length + " users");
            for (int i=0;i<users.length;i++){ 
            out.println("<tr>\n<td>" + users[i].getUserId() +      
                     "</td><td>" + users[i].getName() + 
                     "</td><td>" + users[i].getEmployeeId() + 
                     "</td>\n");

            //Get roles and print               
            System.out.println("User : [ " +i+"]" + users[i].getName());
            Role[] roles = users[i].getRoles();

            out.println("<td>\n");
            for (int j=0;j<roles.length;j++){                
                out.println(roles[j].getName() + "<br>\n");
            }
            out.println("</td>\n");

            }
            out.println("</table>\n");
            
             displayPageFooter(request,response);
        
    }

    public static void displayModifyUserPage(HttpServletRequest request,
            HttpServletResponse response, User[] users) throws IOException{
    
        PrintWriter out = response.getWriter();
        displayPageHeader(request,response);
        
        out.println("<table border=0 width=\"100%\">" +
                    "<tr>\n" + 
                    "<td width=\"200\" valign=\"top\">\n" +
                    "<table class=\"framed\" align=\"center\" width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n" +
                    
                    "<tr>\n<td>\n" +
                    "<a href=\"" + request.getContextPath() + 
                    "/logout\">Logout</a></td>\n</tr>" + 
                    
                    "</table></td>\n<td>\n");
        
        out.println("<h2>Current User List</h2>\n");
        out.println("<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">\n" +
                    "<tr><th>User Id</th><th>Name</th><th>Password</th><th>Employee Id</th><th>Roles</th></tr>\n");
                    
          for (int i=0; i<users.length;i++){
                out.println("<tr>" +
                            "<td><a href=\"" + request.getContextPath() + "/modifyUser?action=showuser&user_id=" + 
                            users[i].getUserId() +  
                            "\">" + users[i].getUserId() + "</a></td>\n" +
                            "<td>" + users[i].getName() + "</td>\n<td>" +
                            users[i].getName() + "</td>\n<td>" + 
                            users[i].getEmployeeId() + "</td><td>"); 
                            
                Role[] roles = users[i].getRoles();
                for (int j=0; j<roles.length;j++){
                    out.println(roles[j].getName() + "<br/>");

                }

              out.println("</td></tr>\n");
             
            
          }
            
            
         out.println("</table>\n"); 
         
         out.println("</td></tr> </table>\n");
         displayPageFooter(request,response);
        

    
    }
    
    public static void displayException(HttpServletRequest request,
            HttpServletResponse response, String message) throws IOException {
    
         PrintWriter out = response.getWriter ();
         displayPageHeader(request,response);
         out.println(message);
         displayPageFooter(request,response);
           
    }
    
    
    
    public static void displayUserForm(HttpServletRequest request,
            HttpServletResponse response, User user,
            HttpServlet servlet) throws IOException {
    
           PrintWriter out = response.getWriter ();
           displayPageHeader(request,response);
           
           
           out.println("<h2>Edit User Details</h2>\n" +
                "<form name=\"userForm\" method=\"post\" action=\"" + request.getContextPath() + "/modifyUser?action=modify\">\n" +

                "<table>\n" +
                "<tr>\n" +
                "<td>User Id:</td>\n" + 
                "<td colspan=\"3\">\n" + user.getUserId() + 

                "<input type=\"hidden\" name=\"userId\" value=\"" + user.getUserId() + "\">\n" +
                "</td></tr>\n<tr><td>Name:</td>\n<td colspan=\"3\">\n" +
                "<input type=\"text\" name=\"name\" size=\"40\" value=\"" + user.getName() +"\">\n" + 
                "</td></tr>\n<tr><td>Password:</td>\n<td colspan=\"3\">\n" +
                "<input type=\"text\" name=\"password\" size=\"40\" value=\"" + user.getPassword() + "\">\n" + 
                "</td></tr>\n<tr><td>Employee Id:</td>\n<td colspan=\"3\">\n" + 
                "<input type=\"text\" name=\"employeeId\" size=\"40\" value=\"" + user.getEmployeeId()+ "\">\n" + 
                "</td></tr>\n<tr><td>Roles:</td>\n<td colspan=\"3\">\n" + 
                "<select name=\"roleIds\" multiple=\"multiple\" size=\"4\">\n");
                
                Role[] roles = user.getRoles();               
                Role[] allRoles = (Role[]) servlet.getServletContext().getAttribute("ALLROLES");
                
                //Loop around all roles and display the pull down menu list for user                
               for (int j=0;j<allRoles.length;j++){
                    out.println("<option value=\"" + allRoles[j].getName() +"\""); 
                    boolean roleFlag = false;
                    for (int i=0;i<roles.length;i++){
                        if (roles[i].getName().equalsIgnoreCase(allRoles[j].getName())){ 
                            roleFlag = true;
                            break;
                        }                      
                        
                    }
                    
                    if (roleFlag){
                        //option box is selected 
                        out.println(" selected=\"selected\" >" + allRoles[j].getName() + "</option>\n"); 
                    
                    }else{
                        out.println(">" + allRoles[j].getName() + "</option>\n");
                        
                    }
                     
                   
                }
                
           out.println("</select>\n");
        
           out.println("</td></tr>\n<tr><td>&nbsp;</td>\n" + 
                "<td colspan=\"2\" align=\"left\"><br></br>\n" +
                "<button type=\"submit\">" +
                "<img src=\"" + request.getContextPath() + "/img/button_submit.gif\" alt=\"\" align=\"middle\"></button>&nbsp;"+ 
                "<a href=\"javascript:history.back();\">" + 
                "<img src=\"" + request.getContextPath() + "/img/button_cancel.gif\" alt=\"\" align=\"absmiddle\" border=\"0\">" +
                "</a>" + 
                "</td>\n" + 
                "</tr>\n" +
                "</table>\n" +
                "</form>\n");
                
           out.println("</td>\n</tr>\n </table>\n");


           displayPageFooter(request,response);
          
      }
      
      
      
  
    
    
         

}